package org.javaymw.springboot.config;

import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;

/**
 * 描述：公共字段处理
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/7/3 11:03
 */
@Component
public class CommonFiledMetaObjectHandler extends MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Timestamp nowTimestamp = new Timestamp(System.currentTimeMillis());
        setCommonFiledValue("tsInsert", nowTimestamp, metaObject);
        setCommonFiledValue("tsUpdate", nowTimestamp, metaObject);
        setCommonFiledValue("dr", 0, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Timestamp nowTimestamp = new Timestamp(System.currentTimeMillis());
        setCommonFiledValue("tsUpdate", nowTimestamp, metaObject);
    }

    private void setCommonFiledValue(String filedName, Object filedValue, MetaObject metaObject) {
        if (metaObject.hasSetter(filedName)) {
            getFieldValByName(filedName, metaObject);
            setFieldValByName(filedName, filedValue, metaObject);
        }
    }
}
